package com.ruanda.config;

import com.ruanda.constant.QueueConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderQueueConfig {

    /**
     * 创建死信队列
     * @return
     */
    @Bean
    public Queue orderQueue(){
        return new Queue(QueueConstants.ORDER_DEAD_QUEUE);
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(QueueConstants.ORDER_DEAD_EX);
    }

    /**
     * 绑定延迟队列
     */
    @Bean
    public Queue orderBinding(){
        Map<String, Object> map = new HashMap<>();
        //配置消息存活时长
        map.put("x-message-ttl", 60*1000);
        //配置消息死后走的交换机
        map.put("x-dead-letter-exchange", QueueConstants.ORDER_DEAD_EX);
        //消息死后走的路由key
        map.put("x-dead-letter-routing-key",QueueConstants.ORDER_DEAD_KEY);
        
        return new Queue(QueueConstants.ORDER_MS_QUEUE,true,false,false,map);
    }

    /**
     * 绑定死信队列
     */
    @Bean
    public Binding orderBinding2(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(QueueConstants.ORDER_DEAD_KEY);
    }
}
